import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;

/**
 * xml多层嵌套 怎么读取转化
 */
public class XMLParser {
    public static void main(String[] args) {
        String xml = "F:\\workspace\\jsd2303\\gitdemo\\XMLdemo\\src\\main\\resources\\delta_utf8.xml";
        try {
            // 创建 SAXReader 对象
            SAXReader reader = new SAXReader();
            // 读取 XML 文件并解析成 Document 对象
            Document document = reader.read(new File(xml));
            // 获取根元素
            Element root = document.getRootElement();
            // 下面的判断应该可以简化？？？ 先实现功能
            Iterator<Element> iterator = root.elementIterator();
            while (iterator.hasNext()) {
                Element element = iterator.next();
                // 处理当前元素（例如打印元素名和属性）
                System.out.println("Element Name111: " + element.getName());
                //  每次判断当前element 是否有子标签
                if (element.elements().size() > 0) {
                    Iterator<Element> nestedIterator = element.elements().iterator();
                    while (nestedIterator.hasNext()) {
                        Element nestedElement = nestedIterator.next();
                        System.out.println("Element Name2222: " + nestedElement.getName());
                        if (nestedElement.elements().size() > 0) {
                            Iterator<Element> threeElement = nestedElement.elements().iterator();
                            while(threeElement.hasNext()){
                                Element element3 = threeElement.next();
                                System.out.println("Element Name3333: " +element3.attributeValue("attrname"));
                                if (element3.elements().size() > 0) {
                                    Iterator<Element> fourElement = nestedElement.elements().iterator();
                                    while(fourElement.hasNext()){
                                        Element element4 = fourElement.next();
                                        System.out.println("Element Name4444: " +element4.attributeValue("attrname"));

                                    }

                                }
                            }

                            }


                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}